package classes;


import db.DriverDAO;
import db.TimeSlotDAO;
import db.UserDAO;
import java.util.ArrayList;
import java.util.List;

public class Driver {

    private int id;
    private String name;
    private String email;
    private String phoneNumber;
    private ArrayList<TimeSlot> timeSlots;
    private static Driver editableDriver;
    private static Driver loggedDriver;
    private ArrayList<TimeSlot> takenSlots;

    //<editor-fold desc="Constructors">
    public Driver() {
    }

    public Driver(int id, String name, String email, String phoneNumber) {
        this.id = id;
        this.name = name;
        this.email = email;
        this.phoneNumber = phoneNumber;

        this.timeSlots = (ArrayList<TimeSlot>)TimeSlotDAO.getTimeSlotsByDriver(id);
    }

    public Driver(int id, String name, String email, String phoneNumber, ArrayList<TimeSlot> timeSlots) {
        this.id = id;
        this.name = name;
        this.email = email;
        this.phoneNumber = phoneNumber;
        this.timeSlots = timeSlots;
    }
    //</editor-fold>

    //<editor-fold desc="Getters and Setters">
    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public ArrayList<TimeSlot> getTimeSlots() {
        return timeSlots;
    }

    public void setTimeSlots(ArrayList<TimeSlot> timeSlots) {
        this.timeSlots = timeSlots;
    }

    public ArrayList<TimeSlot> getTakenSlots() {
        return takenSlots;
    }

    public void setTakenSlots(ArrayList<TimeSlot> takenSlots) {
        this.takenSlots = takenSlots;
    }
    //</editor-fold>

    //<editor-fold desc="Editable Driver">
    public void setEditableDriver(Driver driver) {
        editableDriver = driver;
    }

    //Non-static method for JSF access
    public Driver getEditableDriver() {
        return editableDriver;
    }

    //Static method for Java access
    public static Driver getEditableDriverStatic() {
        return editableDriver;
    }

    //<editor-fold desc="Logged In Driver">
    public void setLoggedDriver(Driver driver) {
        loggedDriver = driver;
    }

    public static void setLoggedDriverStatic(Driver driver) {
        loggedDriver = driver;
    }

    public Driver getLoggedDriver() {
        return loggedDriver;
    }

    public static Driver getLoggedDriverStatic() {
        return loggedDriver;
    }
    //</editor-fold>

    //<editor-fold desc="CRUD Methods">
    public String addDriver() {
        if (DriverDAO.addDriver(this)) {
            List<Driver> drivers = getDrivers();
            if (UserDAO.addUserDriverID(drivers.get(drivers.size() - 1))) {
                Driver.setLoggedDriverStatic(drivers.get(drivers.size() - 1));
                return "addTimeSlotLogged"; //TODO - Forward to time slots
            }
        }
        return "driverAddFailure";
    }

    public String deleteDriver() {
        if (DriverDAO.deleteDriver(this)) {
            return "driverList";
        } else {
            return "driverDeleteFailure";
        }
    }

    public List<Driver> getDrivers() {
        return DriverDAO.getDrivers();
    }

    public String saveDriver() {
        if (DriverDAO.saveDriver(this)) {
            editableDriver = null;
            return "driverList";
        } else {
            return "driverSaveFailure";
        }
    }
    //</editor-fold>

    @Override
    public String toString() {
        return this.name;
    }
}
